<script setup lang="ts">
import type { ConsultType } from '@/enums';
import ConsultItem from './ConsultItem.vue'
import { ref } from 'vue';
import type { ConsultOrderItem, ConsultOrderParams } from '@/types/consult';
import { getConsultOrderPage } from '@/apis/consult';
defineOptions({
  name: 'ConsultList'
})
const props = defineProps<{
  type: ConsultType
}>()
const list = ref<ConsultOrderItem[]>([])
const loading = ref(false)
const finished = ref(false)
const params = ref<ConsultOrderParams>({
  current: 1,
  pageSize: 5,
  type: props.type
})
const onLoad = async () => {
  const res = await getConsultOrderPage(params.value)
  // console.log(res.data.rows);
  list.value.push(...res.data.rows)
  loading.value = false
  if (params.value.current < res.data.pageTotal) {
    params.value.current++
  } else {
    finished.value = true
  }
}
const onDelete = (id: string) => {
  list.value = list.value.filter((item) => item.id !== id)
  if (!list.value.length) onLoad()
}
</script>

<template>
  <div class="consult-list">
    <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <consult-item v-for="item in list" :key="item.id" :item="item" @on-delete="onDelete" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
